﻿'********************************************************************************
'*
'*      DBベークサブ画面画面
'*
'*      File Name : frmDbBakeLotStateCheck.vb
'*      Title     : DBベークサブ画面
'*      機能    　：ラックNoを入力する
'*
'********************************************************************************
'----------------------------------<<修正履歴>>----------------------------------
'ﾊﾞｰｼﾞｮﾝ   日付       作業者        ｺﾒﾝﾄ
'--------- ---------- ------------ -----------------------------------------------
' V1.0.0   2017/06/21 M.Maho   新規作成
' V3.0.0   2017/12/12 M.Sagawa     北京対応
'********************************************************************************

Imports System.Text
Imports ICtagControl

Public Class frmDbBakeLotStateCheck

    'DBベーク画面
    Private frmDBBake As frmStartCompleteDbBake
    Protected m_cEquipMat As New clsDbEquipMat
    Protected m_cLotInfo As New clsDbLotInfo

    'クリックされたボタン名
    Public btnName As String

    Public m_szFrameid_comp_Bake As Integer
    Public szLotNo_Bake As String
    Public szProcsCD_Bake As String


    '配列番号
    Dim idx As Integer

    'ラックNoの文字列
    Dim rackNoStr = {"", "", "", "", ""}

    '不良数関連の定数
    Protected m_cLotDefect As New clsDbLotDefect
    Protected dtError As New DataTable
    Public TotalCount As Integer = 0

    Public Sub New(ByVal fr As frmStartCompleteDbBake)

        MyBase.New()
        frmDBBake = fr
        InitializeComponent()

    End Sub

    'フォームロード時
    Private Sub frmDbBakeLotStateCheck_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'ボタン名から配列番号を取得
        idx = CInt(btnName.Substring(12)) - 1

        '組立ロットNo
        txtLotNo.Text = frmDBBake.dtAllLot.Rows(idx)("LOT_NO")

        '品名
        txtProdName.Text = frmDBBake.dtAllLot.Rows(idx)("PROD_NAME")

        '製品外形
        txtShape.Text = frmDBBake.dtAllLot.Rows(idx)("SHAPE")

        '前工程完成数
        txtPreCompleteQty.Text = frmDBBake.dtAllLot.Rows(idx)("PRE_COMPLETE_NUM")

        '基板・フレーム枚数
        txtPreFrameQty.Text = frmDBBake.dtAllLot.Rows(idx)("PRE_FRAME_NUM")

        '結果
        If Not IsDBNull(frmDBBake.dtAllLot.Rows(idx)("RESULT")) Then
            txtResult.Text = frmDBBake.dtAllLot.Rows(idx)("RESULT")
        End If
        If frmDBBake.dtAllLot.Rows(idx)("LOT_STATE") = frmStartCompleteBase.TagProcessStatus.Ready Then
            '完成数
            txtCompCnt.Text = "0"
            '枚数
            txtPcbNum.Text = "0"
            '不良数
            txtFailCnt.Text = "0"
            'クリアボタン
            btnClear.Enabled = True
        Else
            '完成数
            txtCompCnt.Text = frmDBBake.dtAllLot.Rows(idx)("COMPLETE_NUM")
            '枚数
            txtPcbNum.Text = frmDBBake.dtAllLot.Rows(idx)("FRAME_NUM")
            '不良数
            txtFailCnt.Text = frmDBBake.dtAllLot.Rows(idx)("FAILD_NUM")
            'クリアボタン
            btnClear.Enabled = False
        End If

        If frmDBBake.dtAllLot.Rows(idx)("LOT_STATE") = frmStartCompleteBase.TagProcessStatus.Processing Then
            '枚数
            btnPcbNum.Enabled = True
            '不良数
            btnFailCnt.Enabled = True
        End If

        'ラックNo
        If Not IsDBNull(frmDBBake.dtAllLot.Rows(idx)("RACK_NO_1")) Then
            txtRackNo1.Text = frmDBBake.dtAllLot.Rows(idx)("RACK_NO_1")
        End If

        If Not IsDBNull(frmDBBake.dtAllLot.Rows(idx)("RACK_NO_2")) Then
            txtRackNo2.Text = frmDBBake.dtAllLot.Rows(idx)("RACK_NO_2")
        End If

        If Not IsDBNull(frmDBBake.dtAllLot.Rows(idx)("RACK_NO_3")) Then
            txtRackNo3.Text = frmDBBake.dtAllLot.Rows(idx)("RACK_NO_3")
        End If

        If Not IsDBNull(frmDBBake.dtAllLot.Rows(idx)("RACK_NO_4")) Then
            txtRackNo4.Text = frmDBBake.dtAllLot.Rows(idx)("RACK_NO_4")
        End If

        If Not IsDBNull(frmDBBake.dtAllLot.Rows(idx)("RACK_NO_5")) Then
            txtRackNo5.Text = frmDBBake.dtAllLot.Rows(idx)("RACK_NO_5")
        End If


        '枚数
        AddHandler btnPcbNum.Click, AddressOf tkl_Click
        AddHandler txtPcbNum.MouseHover, AddressOf MouseOverColor
        AddHandler txtPcbNum.MouseMove, AddressOf MouseOverColor

    End Sub

    'OKボタン押下時
    Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click

        Dim checkFlg As Boolean = True

        '前工程ラックNoの数を取得
        Dim preRackCount As Integer = 0
        Dim preRackArray = {"", "", "", "", ""}
        For j = 1 To 5
            Dim preRackNo As String = frmDBBake.dtAllLot.Rows(idx)("PRE_RACK_NO_" + j.ToString).ToString
            ' If preRackNo.Length > 0 AndAlso Not frmStartCompleteBake.DELETE_RACK_NO.Equals(preRackNo) Then '2018/02/13 安部 frmStartCompleteBake ⇒ frmStartCompleteDBBake
            If preRackNo.Length > 0 AndAlso Not frmStartCompleteDbBake.DELETE_RACK_NO.Equals(preRackNo) Then
                preRackArray(preRackCount) = preRackNo
                preRackCount += 1
            End If
        Next

        frmDBBake.dtAllLot.Rows(idx)("RACK_NO_1") = txtRackNo1.Text
        frmDBBake.dtAllLot.Rows(idx)("RACK_NO_2") = txtRackNo2.Text
        frmDBBake.dtAllLot.Rows(idx)("RACK_NO_3") = txtRackNo3.Text
        frmDBBake.dtAllLot.Rows(idx)("RACK_NO_4") = txtRackNo4.Text
        frmDBBake.dtAllLot.Rows(idx)("RACK_NO_5") = txtRackNo5.Text
        If Not frmDBBake.dtAllLot.Rows(idx)("LOT_STATE") = frmStartCompleteBase.TagProcessStatus.Ready Then
            frmDBBake.dtAllLot.Rows(idx)("COMPLETE_NUM") = txtCompCnt.Text
            frmDBBake.dtAllLot.Rows(idx)("FRAME_NUM") = txtPcbNum.Text
            frmDBBake.dtAllLot.Rows(idx)("FAILD_NUM") = txtFailCnt.Text
        End If

        '製造完成フラグ
        frmDBBake.dtAllLot.Rows(idx)("COMP_TYPE") = "3"

        Me.Close()
    End Sub

    'CLEARボタン押下時
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        '初期化
        For i As Integer = 0 To frmDBBake.dtAllLot.Columns.Count - 1
            If TypeOf frmDBBake.dtAllLot.Rows(idx)(i) Is Integer Then
                frmDBBake.dtAllLot.Rows(idx)(i) = 0
            Else
                frmDBBake.dtAllLot.Rows(idx)(i) = String.Empty
            End If
        Next

        Dim BtnLot As Control() = frmDBBake.Controls.Find(btnName, True)
        If BtnLot.Length > 0 Then
            '>> 2018/02/19 安部 固定メッセージをメッセージマスタから取得に変更
            'CType(BtnLot(0), Button).Text = frmDBBake.getMsgStr("MSG00051")
            CType(BtnLot(0), Button).Text = getMsgStr("MSG00051")
            '<< 2018/02/19 安部 固定メッセージをメッセージマスタから取得に変更
            CType(BtnLot(0), Button).Enabled = False
        End If
        Me.Close()
    End Sub

    '不良数ボタン押下時
    Private Sub btnFailCnt_Click(sender As Object, e As EventArgs) Handles btnFailCnt.Click

        '>> 2018/7/19 佐川 ベーク工程不良入力
        Dim iFrameIDUse_Bake As Integer = CInt(m_cEquipMat.getframeIDUse(szLotNo_Bake))

        If m_szFrameid_comp_Bake = 1 And iFrameIDUse_Bake = 1 Then 'UPD 苗 2018/1/31 FrameID_Compが１の場合、AtlasのフレームMAP編集を表示する
            '工程、lotのFrameID管理フラグは両方とも１の場合、ATLASの不良項目画面を呼び出します。
            '外部EXEを呼び出す
            Dim strRetStatus As String = frmStartCompleteBase.Execute_Command(COMMON_PROC_CD, frmStartCompleteBase.EXEC_PG_TAG_BAD, szProcsCD_Bake, g_EQUIP_NO, szLotNo_Bake) 'UPD 苗 2018/1/30
            '戻り値による処理は未定のため、現在は未実装
            If strRetStatus <> "0" Then
                MsgBox(getMsgStr("MSG10009", strRetStatus))
                Exit Sub
            End If
            Call getAtlasUpdatedDetail()
            'UPD 苗 2018/1/31 FrameID_Compが１の場合、AtlasのフレームMAP編集を表示しなくて、SF画面の項目を再表示（フレーム数、X印数、X印枚数、空チップ数）
        ElseIf m_szFrameid_comp_Bake = 2 And iFrameIDUse_Bake = 1 Then
            Call getAtlasUpdatedDetail()
            Exit Sub 'ADD 苗 2018/2/3 
        Else

            Dim frmErrInput As New frmLotCategoryInput
            frmErrInput.LotNo = txtLotNo.Text
            frmErrInput.EquipID = g_CURRENT_EQUIP_ID 'frmDBBake.lblEquipID.Text 'UPD MIAO 2018/8/10
            frmErrInput.KouteiCD = frmDBBake.lblProcessCD.Text
            frmErrInput.StartPosition = FormStartPosition.CenterScreen
            frmErrInput.preProcCompleteCnt = CInt(txtPreCompleteQty.Text)
            frmErrInput.ShowDialog()

            Dim compCnt As Integer = Integer.Parse(frmDBBake.dtAllLot.Rows(idx)("INV_NUM")) - frmErrInput.FailCount
            If compCnt >= 0 Then
                txtCompCnt.Text = compCnt.ToString()
                txtFailCnt.Text = frmErrInput.FailCount

            End If
        End If
        '<< 2018/7/19 佐川 ベーク工程不良入力

    End Sub

    Overridable Sub tkl_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim clickBtn As Button = CType(sender, Button)
        Dim lblInputPos As Label = clickBtn.Parent.Controls.Find(String.Format("txt{0}", clickBtn.Name.Substring(3)), False)(0)

        GetControlItem(Me, lblInputPos)

        Dim pt2Screen As Point = lblInputPos.PointToScreen(New Point(0, -15))
        modTenkey.CallTenkey(lblInputPos, Me, AddressOf TenkeyInputCompleted,
                             pt2Screen.X - Me.Left, pt2Screen.Y - Me.Top)
    End Sub

    Overridable Sub TenkeyInputCompleted()

    End Sub

    Private Sub GetControlItem(Ctrl As Control, ByVal sender As System.Object)
        For Each CtrlItem As Control In Ctrl.Controls
            If TypeOf CtrlItem Is TenKey Then
                CtrlItem.Dispose()
            End If
            If TypeOf CtrlItem Is TenKey Then
                CtrlItem.Dispose()
            End If
            If CtrlItem IsNot sender Then
                If TypeOf CtrlItem Is TenKeyLabel Then
                    CType(CtrlItem, TenKeyLabel).EndEdit()

                ElseIf TypeOf CtrlItem Is Label Then
                    If CtrlItem.BackColor = Color.Cyan Then
                        CtrlItem.BackColor = frmStartCompleteBase.TENKEY_INIT_COLOR
                    End If
                End If
            End If
            If CtrlItem.Controls.Count > 0 Then
                GetControlItem(CtrlItem, sender)
            End If
        Next
    End Sub

    Private Sub MouseOverColor(ByVal sender As Object, ByVal e As EventArgs)
        sender.BackColor = frmStartCompleteBase.NORMAL_INIT_COLOR
    End Sub

    '>> 2018/7/19 佐川 ベーク工程不良入力
    Private Sub getAtlasUpdatedDetail()

        '画面上のフレーム数、X印数、X印枚数、空チップ数を更新する
        Dim dt As DataTable
        dt = m_cLotInfo.getAtlasUpdatedNum(txtLotNo.Text) 'UPD MIAO 2018/9/13
        If dt.Rows.Count <> 0 Then
            'ADD 苗 2018/1/31 
            If dt.Rows(0)("ATLAS_FRAMENUM") = 0 And dt.Rows(0)("ATLAS_BADMARKNUM") = 0 And
               dt.Rows(0)("ATLAS_BADMARK_FRAMENUM") = 0 And dt.Rows(0)("ATLAS_EMPTYCHIPS_NUM") = 0 Then
                'フレーム数、X印数、X印枚数、空チップ数が更新なし
            Else
                '画面項目が存在する項目は値を設定
                txtPcbNum.Text = dt.Rows(0)("ATLAS_FRAMENUM") 'フレーム数
                txtFailCnt.Text = m_cLotDefect.getDefectDataSUM(szLotNo_Bake, frmDBBake.lblProcessCD.Text) '不良数設定　ADD Miao 2018/8/23
                '画面項目が無い項目は変数の値に設定
                frmDBBake.dtAllLot.Rows(idx)("BAD_MARK_NUM") = dt.Rows(0)("ATLAS_BADMARKNUM") 'X印数
                frmDBBake.dtAllLot.Rows(idx)("BAD_MARK_FRMNUM") = dt.Rows(0)("ATLAS_BADMARK_FRAMENUM") 'X印枚数

                If Not IsNothing(frmDBBake.dtAllLot.Rows(idx)("EMPTY_CHIP_NUM")) Then
                    frmDBBake.dtAllLot.Rows(idx)("EMPTY_CHIP_NUM") = dt.Rows(0)("ATLAS_EMPTYCHIPS_NUM") '空チップ数 '苗 2018/2/1  lblCrossPcbCnt => lblEmptyChipsCnt
                End If
            End If
        End If
    End Sub
    '<< 2018/7/19 佐川 ベーク工程不良入力
End Class
